package com.jinzhi.jzapi.examination.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Slf4j
@Component
public class AutomaticScoringJob implements Job {

    @Autowired
    private StuSignUpService stuSignUpService;
    @Autowired
    private ExaminationResultsService examinationResultsService;
    @Autowired
    private ExaminationBatchService examinationBatchService;
    @Autowired
    private ExaminationSingleScoreService examinationSingleScoreService;

    @Override
    @Transactional
    public void execute(JobExecutionContext jobExecutionContext) {
        log.info("---------------------开始重置测试考试批次---------------------");
        //查询测试批次
        EntityWrapper<ExaminationBatchDO> ew = new EntityWrapper<ExaminationBatchDO>();
        ew.eq("name","测试批次");
        ExaminationBatchDO examinationBatchDOS = examinationBatchService.selectOne(ew);
        if(examinationBatchDOS != null){
            examinationBatchDOS.setState(6);
            Date now = new Date();
            Calendar start = Calendar.getInstance();
            start.setTime(now);
            // 将时分秒,毫秒域清零
            start.set(Calendar.HOUR_OF_DAY, 8);
            start.set(Calendar.MINUTE, 0);
            start.set(Calendar.SECOND, 0);
            examinationBatchDOS.setStartTime(start.getTime());
            Calendar end = Calendar.getInstance();
            end.setTime(now);
            // 将时分秒,毫秒域清零
            end.set(Calendar.HOUR_OF_DAY, 22);
            end.set(Calendar.MINUTE, 0);
            end.set(Calendar.SECOND, 0);
            examinationBatchDOS.setEndTime(end.getTime());
            examinationBatchService.updateById(examinationBatchDOS);
        }
//        log.info("---------------------开始计算技能考试成绩---------------------");
//        //第一步，获取近三日技能考试批次信息
//        Map map = new HashMap();
//        map.put("type", 1);
//        map.put("days", 3);
//        List<ExaminationBatchDO> examinationBatchDOS = examinationBatchService.selListExamBatch(map);
//        if (examinationBatchDOS != null && examinationBatchDOS.size() > 0) {
//            for (ExaminationBatchDO examinationBatchDO : examinationBatchDOS) {
//                //根据考试批次id对应的认证批次id,查询对应的所有考生信息
//                List<StuSignUpDO> stuSignUpDOS = stuSignUpService.findByKv("cbatch_id", examinationBatchDO.getCbatchId());
//                if (stuSignUpDOS != null && stuSignUpDOS.size() > 0) {
//                    for (StuSignUpDO stuSignUpDO : stuSignUpDOS) {
//                        //根据考生信息id和考试批次获得考生此次技能考试的分数；汇总所有考评员单项分数后的平均值
//                        map.clear();
//                        map.put("stuSignId", stuSignUpDO.getId());
//                        map.put("ebatchId", examinationBatchDO.getId());
//                        Double stuScores = examinationSingleScoreService.selStuScore(map);//考生此次技能考试的分数
//
//                        ExaminationResultsDO examinationResultsDOss = examinationResultsService
//                                .findOneByKv("cbatch_id", examinationBatchDO.getCbatchId(), "ebatch_id", examinationBatchDO.getId(), "stu_sign_id", stuSignUpDO.getId());
//                        if (examinationResultsDOss == null) {
//                            ExaminationResultsDO examinationResultsDO = new ExaminationResultsDO();
//                            examinationResultsDO.setCbatchId(stuSignUpDO.getCbatchId());//认证批次id
//                            examinationResultsDO.setEbatchId(examinationBatchDO.getId());//考试批次id
//                            examinationResultsDO.setStuSignId(stuSignUpDO.getId());//考生信息id
//                            examinationResultsDO.setState(0);//正常
//                            examinationResultsDO.setTotalScore(stuScores);//考试成绩
//                            boolean b = examinationResultsService.insert(examinationResultsDO);
//                            if (!b) {
//                                log.info("--------认证批次：" + stuSignUpDO.getCbatchId() + "，中的考试批次：" + examinationBatchDO.getId() + "，下的考生：" + stuSignUpDO.getName() + "的成绩录入失败！");
//                            }
//                        }
//                    }
//                }
//            }
//        }
    }

}
